﻿define(['jquery', 'kendo', 'moment', 'ajax-pre'], function ($, kendo, moment) {
    var userId = "123";
    //var departments = JSON.parse(document.getElementById('t').value);

    var dataSource = new kendo.data.DataSource({
        schema: {
            data: function (response) {
                return response;
            },
            total: function (response) {
                return response.Total;
            },
            model: {
                id: "Id",
                fields: {
                    Id: { nullable: false },
                    QualName: { validation: { required: true } },
                    QualType: { validation: { required: true } },
                    QualLevel: { validation: { required: true } },
                    ValidStartDate: { validation: { required: true }, defaultValue: new Date() },
                    ValidEndDate: { validation: { required: true }, defaultValue: new Date() },
                    Remark: { validation: { required: false }, defaultValue: null },
                    UserId: { validation: { required: true }, defaultValue: userId },
                }
            }
        },
        pageSize: 12,
        serverPaging: true,
        batch: true,
        transport: {
            read: {
                url: "/Qualification/Get",
                dataType: "json",
                method: "post"
            },
            update: {
                url: "/Qualification/Save",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            destroy: {
                url: "/Qualification/Remove",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            create: {
                url: "/Qualification/Save",
                dataType: "json",
                method: "post",
                contentType: "application/json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return JSON.stringify({ entitys: options.models });
                }
                if (operation == "read") {
                    return {
                        page: {
                            page: options.page,
                            pages: options.pageSize
                        }
                    };
                }
            }
        }
    });

    $("#grid").kendoGrid({
        dataSource: dataSource,
        filterable: true,
        toolbar: [{ name: "create", text: '新建' }, { name: "save", text: '保存' }],
        pageable: {
            refresh: true,
            pageSizes: true,
            buttonCount: 5
        },
        columns: [
        { field: "QualName", title: "资质名", width: '180px' },
        { field: "QualType", title: "资质类型(语种)" },
        { field: "QualLevel", title: "资质等级" },
        {
            field: "ValidStartDate", title: "有效期开始", width: '150px',
            template: function (value) {
                return new moment(value.ValidStartDate).format('YYYY-MM-DD');
            },
            editor: function (container, options) {
                $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                .kendoDatePicker({
                    format: 'yyyy-MM-dd',
                    interval: 1
                });
            }
        },
         {
             field: "ValidEndDate", title: "有效期结束", width: '150px',
             template: function (value) {
                 return new moment(value.ValidEndDate).format('YYYY-MM-DD');
             },
             editor: function (container, options) {
                 $('<input data-bind="value:' + options.field + '" />').appendTo(container)
                 .kendoDatePicker({
                     format: 'yyyy-MM-dd',
                     interval: 1
                 });
             }
         },
         { field: "Remark", title: "备注" },
        {
            width: '250px',
            command: [
              { name: "destroy", 'text': '删除' },
              { name: "edit", text: '编辑' }
            ]
        }
        ],
        editable: { //disables the deletion functionality
            update: true,
            destroy: true,
            create: true,
            mode: 'inline',// inline popup
        },
        //editable: "popup",
        pageable: true,

    });
});